﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PhoneCore.Framework.Diagnostic.Tracing;
using PhoneCore.Framework.Storage;
using SecureBox.UI.Infrastructure.Model;

namespace SecureBox.UI.Infrastructure.Data
{
    /// <summary>
    /// Represents a repository for Property entity which uses Linq DataContext
    /// </summary>
    public class PropertyRepository: IRepository<Model.Property>
    {
        private readonly SecureBoxDataContext _dataContext;
        public PropertyRepository(SecureBoxDataContext dataContext)
        {
            _dataContext = dataContext;
        }

        /// <summary>
        /// Add entity to repository
        /// </summary>
        /// <param name="entity"></param>
        public void Add(Model.Property entity)
        {
            var dataRecord = _dataContext.Records.Single(r => r.Id == entity.Id);
            _dataContext.Properties.InsertOnSubmit(new Property() { Name = entity.Name, Value = entity.Value, Record = dataRecord});
        }

        /// <summary>
        /// Gets entity using predicate provided
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public Model.Property Get(Expression<Func<Model.Property, bool>> predicate)
        {
            return (from Data.Property p in _dataContext.Properties
                    select CreateFromData(p)).Where(predicate).Single();
        }

        /// <summary>
        /// Removes entites from repository using predicate provided
        /// </summary>
        /// <param name="predicate"></param>
        public void Remove(Expression<Func<Model.Property, bool>> predicate)
        {
            var modelProperties = (from Data.Property p in _dataContext.Properties
                                 select CreateFromData(p)).Where(predicate);

            var dataProperties = modelProperties.Select(p => _dataContext.Properties.Single(pd => pd.Id == p.Id));
            _dataContext.Properties.DeleteAllOnSubmit(dataProperties);
        }

        /// <summary>
        /// Returns all entities from repository
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Model.Property> List()
        {
            return (from Data.Property p in _dataContext.Properties
                    select CreateFromData(p));
        }

        /// <summary>
        /// Returns entitites using predicate provided
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public IEnumerable<Model.Property> List(Expression<Func<Model.Property, bool>> predicate)
        {
            return (from Data.Property p in _dataContext.Properties
                    select CreateFromData(p)).Where(predicate);
        }

        /// <summary>
        /// Commit changes
        /// </summary>
        public void Commit()
        {
            _dataContext.SubmitChanges();
        }

        public static Model.Property CreateFromData(Data.Property property)
        {
            return new Model.Property()
            {
                Id = property.Id,
                Name = property.Name,
                Value = property.Value,
                //Record = RecordRepository.CreateFromData(property.Record)
            };
        }


        public void Update(Model.Property entity)
        {
            throw new NotImplementedException();
        }
    }
}
